package com.springboot.demo.controller;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userNew")
public class userNew {
    @RequestMapping("/login")
    public Boolean login(String userName, String passWord, HttpSession session){
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(passWord)){
            return false;
        }

        if("admin".equals(userName) && "admin".equals(passWord)){
            session.setAttribute("loginUser",userName);
            return true;
        }
        return false;
    }

//    @RequestMapping("loginUser")
//    public String loginUser(HttpSession session){
//        String loginName= (String) session.getAttribute("loginUser");
//        return loginName;
//    }

    @RequestMapping("/loginUser")
    public String loginUser(HttpServletRequest  request){
        //参数为false，获取的session为null时，不会创建session
        HttpSession session= request.getSession(false);

        if(session!=null){
            String loginName=(String)session.getAttribute("loginUser");
            return loginName;
        }
        return "";
    }
}
